﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Data.Model;using NHibernate.Criterion;
using System.Collections.Generic;
using NHibernate;

namespace ZLCMS.Web.Example
{
    public partial class DocViewTemplate : System.Web.UI.Page
    {
        private string _kind = "Test";//对应的 DOC_CONFIG 的分类

        protected DocInfo _docDb;
        protected DocInfo _docInfo_pre;
        protected DocInfo _docInfo_next;

        protected void Page_Load(object sender, EventArgs e)
        {
            using (ISession iNHSession = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                if (!string.IsNullOrEmpty(Request["uid"]))
                {
                    //根据ID获取数据
                    _docDb = iNHSession.CreateCriteria<DocInfo>().Add(Expression.Eq("Kind", _kind)).Add(Expression.Eq("Id", int.Parse(Request["uid"]))).UniqueResult<DocInfo>();
                }

                //仍然没有数据跳转首页
                if (_docDb == null)
                {
                    Response.Redirect("~/");
                }

                //初始化上一页、下一页代码
                IList<DocInfo> list;

                list = iNHSession.CreateCriteria<DocInfo>().Add(Expression.Eq("Kind", _kind)).Add(Expression.Lt("Id", _docDb.Id)).AddOrder(Order.Desc("Id")).SetMaxResults(1).List<DocInfo>();

                if (list.Count > 0)
                {
                    _docInfo_pre = list[0];
                }
                else
                {
                    _docInfo_pre = null;
                }

                list = iNHSession.CreateCriteria<DocInfo>().Add(Expression.Eq("Kind", _kind)).Add(Expression.Gt("Id", _docDb.Id)).AddOrder(Order.Asc("Id")).SetMaxResults(1).List<DocInfo>();

                if (list.Count > 0)
                {
                    _docInfo_next = list[0];
                }
                else
                {
                    _docInfo_next = null;
                }
            }
        }

        /// <summary>
        /// 上一页、下一页代码
        /// </summary>
        /// <returns></returns>
        protected string GetPreNextLink()
        {
            string retString = "";

            if (_docInfo_pre != null)
            {
                retString = "<span class=\"fl\"><<<a href=\"/Team/AgentView.aspx?uid=" + _docInfo_pre.Id.ToString() + "\">&nbsp;上一个&nbsp;</a></span>";
            }
            else
            {
                //retString = "<span class=\"fl\"><<&nbsp;上一个&nbsp;没有了</span>";
            }

            if (_docInfo_next != null)
            {
                retString += "<font class=\"fr\"><a href=\"/Team/AgentView.aspx?uid=" + _docInfo_next.Id.ToString() + "\">下一个&nbsp;</a>>></font>";
            }
            else
            {
                //retString += "<font class=\"fr\">下一个&nbsp;没有了&nbsp;>></font>";
            }
            return retString;
        }
    }
}
